import os
from steam_api_client import SteamAPIClient
from models import Game
from visualizer import GameDataVisualizer

def main():
    """主程序入口"""
    # 创建输出目录
    output_dir = 'output'
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    
    # 初始化客户端
    client = SteamAPIClient()
    
    try:
        # 获取畅销游戏数据
        print("正在获取Steam畅销游戏数据...")
        top_sellers = client.fetch_top_sellers()
        
        # 转换为Game对象列表
        games = [Game.from_dict(game_data) for game_data in top_sellers]
        
        # 打印游戏信息
        print("\nSteam畅销游戏排行榜：")
        print("-" * 100)
        print(f"{'排名':<6}{'游戏名称':<50}{'价格':<15}{'折扣':<10}")
        print("-" * 100)
        
        for idx, game in enumerate(games, 1):
            price_str = f"¥{game.price:.2f}" if game.price > 0 else "未公布"
            print(f"{idx:<6}{game.name:<50}{price_str:<15}{f'-{game.discount}%':<10}")
        
        # 生成可视化图表
        print("\n正在生成数据可视化图表...")
        visualizer = GameDataVisualizer()
        
        # 价格分布图
        visualizer.plot_price_distribution(
            games,
            save_path=os.path.join(output_dir, 'price_distribution.png')
        )
        
        # 折扣分布图
        visualizer.plot_discount_distribution(
            games,
            save_path=os.path.join(output_dir, 'discount_distribution.png')
        )
        
        # 价格最高的游戏
        visualizer.plot_top_games_by_price(
            games,
            top_n=10,
            save_path=os.path.join(output_dir, 'top_games_by_price.png')
        )
        
        print(f"\n图表已保存到 {output_dir} 目录")
        
    except Exception as e:
        print(f"发生错误: {str(e)}")

if __name__ == "__main__":
    main() 